package org.example.est.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.example.est.dto.MaintainOrderDTO;
import org.example.est.entity.PayOrder;
import org.example.est.common.exception.BusinessException;
import org.example.est.constants.Constants;
import org.example.est.constants.MaintainStatusEnum;
import org.example.est.constants.ResultEnum;
import org.example.est.service.AliPayService;
import org.example.est.service.MaintainOrderService;
import org.example.est.service.PayOrderService;
import org.example.est.service.util.AliPayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Service
public class AliPayServiceImpl implements AliPayService {
    @Autowired
    private AliPayProperties aliPayProperties;
    @Autowired
    private MaintainOrderService maintainOrderService;
    @Autowired
    private PayOrderService payOrderService;
    @Override
    public String paySubscription(String orderNo) throws AlipayApiException {
        MaintainOrderDTO maintainOrderDTO = maintainOrderService.getMaintainOrderBy(orderNo);
        if(ObjectUtils.isEmpty(maintainOrderDTO)){
            throw new BusinessException(ResultEnum.FAIL_PARAM);
        }
        if(!MaintainStatusEnum.APPOINT.getCode().equals(maintainOrderDTO.getStatus())){
            throw new BusinessException(ResultEnum.FAIL_PARAM);
        }
        // 创建支付订单
        PayOrder payOrder = new PayOrder();
        payOrder.setOrderno(maintainOrderDTO.getOrderNo());
        String payNo= maintainOrderDTO.getOrderNo()+ RandomUtil.randomNumbers(5);
        payOrder.setPayno(payNo);
        // payOrder.setAliTradeNo();
        payOrder.setUserid(maintainOrderDTO.getUserId());
        payOrder.setPrice(maintainOrderDTO.getSubscription());
        // 1:定金,2:尾款
        payOrder.setPaytype(1);
        payOrder.setStatus(Constants.Order.PayStaus.UN_PAY);
        payOrder.setPaychannel(Constants.Order.Channel.ALIPAY);
        payOrder.setCreateduserid(maintainOrderDTO.getUserId());
        payOrder.setCreatedtime(new Date());
        payOrder.setIsdelete(Constants.EST_NOT);
        payOrderService.addPayOrder(payOrder);

        //获得初始化的AlipayClient
        AlipayClient alipayClient =
                new DefaultAlipayClient(aliPayProperties.getGatewayUrl(),
                        aliPayProperties.getAppId(),
                        aliPayProperties.getMerchantPrivateKey(),
                        "json",
                        aliPayProperties.getCharset(),
                        aliPayProperties.getAlipayPublicKey(),
                        aliPayProperties.getSignType());
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(aliPayProperties.getReturnUrl());
        alipayRequest.setNotifyUrl(aliPayProperties.getNotifyUrl());

        JSONObject jsonObject = new JSONObject();
        // 支付订单号要唯一，用于和支付宝对账
        jsonObject.set("out_trade_no",payNo);
        jsonObject.set("total_amount",payOrder.getPrice());
        jsonObject.set("subject","商品维修定金");
        jsonObject.set("body","");
        jsonObject.set("product_code","FAST_INSTANT_TRADE_PAY");

        alipayRequest.setBizContent(jsonObject.toString());
        String body = alipayClient.pageExecute(alipayRequest).getBody();
        return body;
    }
}
